package com.xbongbong.paas.service.log.crm.operation.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.service.log.crm.help.OperationLogHelp;
import com.xbongbong.paas.service.log.crm.operation.AbstractCrmOperationLogHandle;
import com.xbongbong.parent.interfaces.logger.ArchiveOperationLogHandle;
import com.xbongbong.parent.interfaces.logger.PushNotifyLogHandle;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.pro.log.pojo.dto.AddOperationLogDTO;
import com.xbongbong.pro.log.pojo.dto.DeletePushNotifyDTO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/4/15 11:29
 * @description:报价单操作日志
 */
@Slf4j
@Service("quotationOperationLogHandleImpl")
public class QuotationOperationLogHandleImpl extends AbstractCrmOperationLogHandle
        implements PushNotifyLogHandle , ArchiveOperationLogHandle {
    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.QUOTATION.getCode());
    }

    @Override
    @Async("sysLogThreadPool")
    public void deletePushNotify(DeletePushNotifyDTO deletePushNotifyDTO) {
        deletePushNotify(deletePushNotifyDTO, QuotationEnum.NAME.getAttr());
    }

    @Override
    @Async("sysLogThreadPool")
    public void add(AddOperationLogDTO addOperationLogDTO) {
        try {
            FormDataAddDTO formDataAddDTO = addOperationLogDTO.getFormDataAddDTO();
            PaasFormDataEntity newPaasFormDataEntity = addOperationLogDTO.getNewPaasFormDataEntity();
            SaveVO saveVO = addOperationLogDTO.getSaveVO();
            BeforeSaveVO beforeSaveVO = addOperationLogDTO.getBeforeSaveVO();
            boolean isImport = Objects.equals(1, formDataAddDTO.getIsImport());
            JSONObject data = newPaasFormDataEntity.getData();
            String version = FastJsonHelper.getStringOrDefaultFromFormData(data, QuotationEnum.VERSION.getAttr(), "");
            Boolean newVersion = false;
            if (!Objects.equals(version, StringConstant.DEFAULT_VERSION)) {
                newVersion = true;
            }
            if (isImport || newVersion) {
                return;
            }
            String logName = OperationLogHelp.getName(type(),data);
            String memoName = beforeSaveVO.getPaasFormEntityExt().getName();
            addSaveLogger(formDataAddDTO, newPaasFormDataEntity, logName, memoName, OperateModuleTypeEnum.QUOTATION, saveVO, beforeSaveVO);
        } catch (XbbException e) {
            log.error("com.xbongbong.paas.service.log.operation.impl.QuotationOperationLogHandleImpl.add fail", e);
        }
    }
}
